package cn.iocoder.yudao.module.jky.controller.admin.goodsattribute;

import cn.iocoder.yudao.module.jky.convert.goodsattributevalue.GoodsAttributeValueConvert;
import cn.iocoder.yudao.module.jky.dal.dataobject.goodsattributevalue.GoodsAttributeValueDO;
import cn.iocoder.yudao.module.jky.service.goodsattributevalue.GoodsAttributeValueService;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.operatelog.core.annotations.OperateLog;
import static cn.iocoder.yudao.framework.operatelog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.jky.controller.admin.goodsattribute.vo.*;
import cn.iocoder.yudao.module.jky.dal.dataobject.goodsattribute.GoodsAttributeDO;
import cn.iocoder.yudao.module.jky.convert.goodsattribute.GoodsAttributeConvert;
import cn.iocoder.yudao.module.jky.service.goodsattribute.GoodsAttributeService;

@Tag(name = "管理后台 - 商品属性")
@RestController
@RequestMapping("/supplier/goods-attribute")
@Validated
public class GoodsAttributeController {

    @Resource
    private GoodsAttributeService goodsAttributeService;

    @Resource
    private GoodsAttributeValueService goodsAttributeValueService;

    @PostMapping("/create")
    @Operation(summary = "创建商品属性")
    @PreAuthorize("@ss.hasPermission('jky:goods-attribute:create')")
    public CommonResult<Short> createGoodsAttribute(@Valid @RequestBody GoodsAttributeCreateReqVO createReqVO) {
        return success(goodsAttributeService.createGoodsAttribute(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新商品属性")
    @PreAuthorize("@ss.hasPermission('jky:goods-attribute:update')")
    public CommonResult<Boolean> updateGoodsAttribute(@Valid @RequestBody GoodsAttributeUpdateReqVO updateReqVO) {
        goodsAttributeService.updateGoodsAttribute(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除商品属性")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('jky:goods-attribute:delete')")
    public CommonResult<Boolean> deleteGoodsAttribute(@RequestParam("id") Short id) {
        goodsAttributeService.deleteGoodsAttribute(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得商品属性")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('jky:goods-attribute:query')")
    public CommonResult<GoodsAttributeRespVO> getGoodsAttribute(@RequestParam("id") Short id) {
        GoodsAttributeDO goodsAttribute = goodsAttributeService.getGoodsAttribute(id);
        return success(GoodsAttributeConvert.INSTANCE.convert(goodsAttribute));
    }

    @GetMapping("/list")
    @Operation(summary = "获得商品属性列表")
    @Parameter(name = "ids", description = "编号列表", required = true, example = "1024,2048")
    @PreAuthorize("@ss.hasPermission('jky:goods-attribute:query')")
    public CommonResult<List<GoodsAttributeRespVO>> getGoodsAttributeList(@RequestParam("id") Short catId) {
        List<GoodsAttributeDO> list = goodsAttributeService.getGoodsAttributeList(catId);
        list.forEach(item -> {
            List<GoodsAttributeValueDO> goodsAttributeValueList = goodsAttributeValueService.getGoodsAttributeValueList(item.getGoodsAttributeId());
            item.setValueList(GoodsAttributeValueConvert.INSTANCE.convertList(goodsAttributeValueList));
        });
        return success(GoodsAttributeConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/page")
    @Operation(summary = "获得商品属性分页")
    @PreAuthorize("@ss.hasPermission('jky:goods-attribute:query')")
    public CommonResult<PageResult<GoodsAttributeRespVO>> getGoodsAttributePage(@Valid GoodsAttributePageReqVO pageVO) {
        PageResult<GoodsAttributeDO> pageResult = goodsAttributeService.getGoodsAttributePage(pageVO);
        return success(GoodsAttributeConvert.INSTANCE.convertPage(pageResult));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出商品属性 Excel")
    @PreAuthorize("@ss.hasPermission('jky:goods-attribute:export')")
    @OperateLog(type = EXPORT)
    public void exportGoodsAttributeExcel(@Valid GoodsAttributeExportReqVO exportReqVO,
              HttpServletResponse response) throws IOException {
        List<GoodsAttributeDO> list = goodsAttributeService.getGoodsAttributeList(exportReqVO);
        // 导出 Excel
        List<GoodsAttributeExcelVO> datas = GoodsAttributeConvert.INSTANCE.convertList02(list);
        ExcelUtils.write(response, "商品属性.xls", "数据", GoodsAttributeExcelVO.class, datas);
    }

}
